const express = require('express');
const router = express.Router();
const con = require('../db');
const con2 = require('../db/mysql');
//RESTFul 风格 api
//获取 get /role
//新增 post /role
//修改 put /role/:id
//删除 delete /role/:id
//单个查询 get /role/:id
router.get('/', async (req, res) => {
    const { page, limit } = req.query;
    try {
        const result = await con2.query(`SELECT * FROM role ORDER BY id DESC LIMIT ${limit} OFFSET ${page * limit - limit};SELECT COUNT(*) AS total FROM role`);
        if (result.length) {
            res.json({
                msg: "查询成功",
                code: 200,
                data: result[0],
                total: result[1][0].total
            });
        }
    } catch (error) {
        throw error;
    }
    // con.query(`SELECT * FROM role ORDER BY id DESC LIMIT ${limit} OFFSET ${page * 5 - 5};SELECT COUNT(*) AS total FROM role`, (err, result) => {
    //     if (err) throw err;
    //     res.json({
    //         msg: "查询成功",
    //         code: 200,
    //         data: result[0],
    //         total: result[1][0].total
    //     });
    // })
})
router.post("/", async (req, res) => {
    const { rolename } = req.body;
    try {
        const result = await con2.query(`SELECT * FROM role WHERE rolename=?`, rolename);
        if (result.length > 0) {
            res.json({
                msg: "存在重复角色名称",
                code: 500,
            })
            return;
        }
        const result2 = await con2.query(`INSERT INTO role (rolename) VALUES (?)`, rolename);
        if (result2.affectedRows === 1) {
            res.json({
                msg: "新增成功",
                code: 200,
            })
        } else {
            res.json({
                msg: "新增失败",
                code: 500,
            })
        }
    }
    catch (e) {
        throw e;
    }
    // con.query(`SELECT * FROM role WHERE rolename=?`, rolename, (err, result) => {
    //     if (err) throw err;
    //     if (result.length > 0) {
    //         res.json({
    //             msg: "存在重复角色名称",
    //             code: 500,
    //         })
    //     } else {
    //         con.query(`INSERT INTO role (rolename) VALUES (?)`, rolename, (err, result) => {
    //             if (err) throw err;
    //             if (result.affectedRows === 1) {
    //                 res.json({
    //                     msg: "新增成功",
    //                     code: 200,
    //                 })
    //             } else {
    //                 res.json({
    //                     msg: "新增失败",
    //                     code: 500,
    //                 })
    //             }
    //         })
    //     }
    // })
})
router.delete('/:id', async (req, res) => {
    const { id } = req.params;
    try {
        const result=await con2.query(`DELETE FROM role WHERE id=?`, id);
        if (result.affectedRows === 1) {
            res.json({
                msg: "删除成功",
                code: 200,
            })
        } else {
            res.json({
                msg: "删除失败",
                code: 500,
            })
        }
    } catch (error) {
        throw error;
    }
    // con.query(`DELETE FROM role WHERE id=?`, id, (err, result) => {
    //     if (err) throw err;
    //     if (result.affectedRows === 1) {
    //         res.json({
    //             msg: "删除成功",
    //             code: 200,
    //         })
    //     } else {
    //         res.json({
    //             msg: "删除失败",
    //             code: 500,
    //         })
    //     }
    // })
})
router.put('/:id', async (req, res) => {
    const { id } = req.params;
    const { state, rolename,menu } = req.body;
    try {
        if (state !== undefined) {
            const result=await con2.query(`UPDATE role SET state=? WHERE id=?`, [state, id]);
            if (result.affectedRows === 1) {
                res.json({
                    msg: "更新成功",
                    code: 200,
                })
            } else {
                res.json({
                    msg: "更新失败",
                    code: 500,
                })
            }
        } else if (rolename !== undefined) {
            const result=await con2.query(`SELECT * FROM role WHERE rolename=?`, rolename);
            if (result.length) {
                res.json({
                    msg: "存在重复角色名称或未更新角色名",
                    code: 500,
                })
                return;
            }
            const result2=await con2.query(`UPDATE role SET rolename=? WHERE id=?`, [rolename, id]);
            if (result2.affectedRows === 1) {
                res.json({
                    msg: "更新成功",
                    code: 200,
                })
            } else {
                res.json({
                    msg: "更新失败",
                    code: 500,
                })
            }
        }else if(menu!=undefined){
            const result=await con2.query(`UPDATE role SET menu=? WHERE id=?`, [menu, id]);
            if (result.affectedRows === 1) {
                res.json({
                    msg: "更新成功",
                    code: 200,
                })
            } else {
                res.json({
                    msg: "更新失败",
                    code: 500,
                })
            }
        }
    } catch (error) {
        throw error;
    }
    // if (state !== undefined) {
    //     con.query(`UPDATE role SET state=? WHERE id=?`, [state, id], (err, result) => {
    //         if (err) throw err;
    //         if (result.affectedRows === 1) {
    //             res.json({
    //                 msg: "更新成功",
    //                 code: 200,
    //             })
    //         } else {
    //             res.json({
    //                 msg: "更新失败",
    //                 code: 500,
    //             })
    //         }
    //     })
    // } else if (rolename !== undefined) {
    //     con.query(`SELECT * FROM role WHERE rolename=?`, rolename, (err, result) => {
    //         if (err) throw err;
    //         if (result.length) {
    //             res.json({
    //                 msg: "存在重复角色名称或未更新角色名",
    //                 code: 500,
    //             })
    //         } else {
    //             con.query(`UPDATE role SET rolename=? WHERE id=?`, [rolename, id], (err, result) => {
    //                 if (err) throw err;
    //                 if (result.affectedRows === 1) {
    //                     res.json({
    //                         msg: "更新成功",
    //                         code: 200,
    //                     })
    //                 } else {
    //                     res.json({
    //                         msg: "更新失败",
    //                         code: 500,
    //                     })
    //                 }
    //             })
    //         }
    //     })
    // }
})
router.get('/:id', async (req, res) => {
    const { id } = req.params;
    try {
        const result = await con2.query(`SELECT * FROM role WHERE id=?`,id);
        if (result.length) {
            res.json({
                msg: "查询成功",
                code: 200,
                data: result[0],
            });
        }
    } catch (error) {
        throw error;
    }
})
module.exports = router;